using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using JlPay.Sdk.Models;
using JlPay.Sdk.Services;
using System;

namespace JlPay.Sdk.Models.Openmerch.Device
{
    /// <summary>
    /// 银联码查询响应
    /// </summary>
    public class UnionPayCodeQueryResponse : StandardResponse, IComplexFieldHandler
    {
        /// <summary>
        /// 二维码列表
        /// </summary>
        [JsonPropertyName("qrcodes")]
        public List<Dictionary<string, object>>? Qrcodes { get; set; }

        /// <summary>
        /// 处理复杂字段
        /// </summary>
        /// <param name="fieldName">字段名</param>
        /// <param name="fieldValue">字段值</param>
        public void HandleComplexField(string fieldName, object fieldValue)
        {
            if (fieldName == "qrcodes")
            {
                if (fieldValue is JsonElement jsonElement)
                {
                    if (jsonElement.ValueKind == JsonValueKind.Array)
                    {
                        Qrcodes = new List<Dictionary<string, object>>();
                        foreach (var item in jsonElement.EnumerateArray())
                        {
                            if (item.ValueKind == JsonValueKind.Object)
                            {
                                var dict = JsonSerializer.Deserialize<Dictionary<string, object>>(item.GetRawText(), new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower });
                                if (dict != null) Qrcodes.Add(dict);
                            }
                        }
                    }
                }
                else if (fieldValue is System.Collections.IEnumerable enumerable)
                {
                    Qrcodes = new List<Dictionary<string, object>>();
                    foreach (var item in enumerable)
                    {
                        if (item is Dictionary<string, object> dict)
                        {
                            Qrcodes.Add(dict);
                        }
                    }
                }
                else if (fieldValue is string jsonString)
                {
                    try
                    {
                        var qrcodes = JsonSerializer.Deserialize<List<Dictionary<string, object>>>(jsonString, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower });
                        Qrcodes = qrcodes;
                    }
                    catch
                    {
                        // 解析失败，保持为null
                    }
                }
            }
        }

        /// <summary>
        /// 获取二维码列表
        /// </summary>
        /// <returns>二维码列表</returns>
        public List<Dictionary<string, object>> GetQrcodeList()
        {
            return Qrcodes ?? new List<Dictionary<string, object>>();
        }

        /// <summary>
        /// 获取第一个二维码信息
        /// </summary>
        /// <returns>第一个二维码信息</returns>
        public Dictionary<string, object>? GetFirstQrcode()
        {
            if (Qrcodes != null && Qrcodes.Count > 0)
            {
                return Qrcodes[0];
            }
            return null;
        }

        /// <summary>
        /// 根据终端号获取二维码信息
        /// </summary>
        /// <param name="termNo">终端号</param>
        /// <returns>二维码信息</returns>
        public Dictionary<string, object>? GetQrcodeByTermNo(string termNo)
        {
            if (Qrcodes != null)
            {
                foreach (var qrcode in Qrcodes)
                {
                    if (qrcode.TryGetValue("term_no", out var value) && value?.ToString() == termNo)
                    {
                        return qrcode;
                    }
                }
            }
            return null;
        }
    }
} 